//
//  HomeSearchVC.m
//  NewPro
//
//  Created by 何荧红 on 2019/4/10.
//  Copyright © 2019 何荧红. All rights reserved.
//

#import "HomeSearchVC.h"
#import "HomeSearchCell.h"

#import "MyPOSlistVC.h"//划拨
#import "POSCanTransferVC.h"//回拨
#import "TraditionPOSListVC.h"//POS管理
#import "NewWalletViewController.h"//钱包
#import "BandViewController.h"//银行卡
#import "MyProfitViewController.h"//我的收益
#import "PutForwardRecordViewController.h"//提现记录

@interface HomeSearchVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UIView *titleView;
@property (nonatomic, strong) UITextField *Search;

@property (nonatomic, strong) UITableView *tableView;
// 数据源数组
@property (nonatomic, strong) NSMutableArray *dataArr;
// 搜索结果数组
@property (nonatomic, strong) NSMutableArray *results;

@property (nonatomic, strong) NSDictionary *datadic;

@end

@implementation HomeSearchVC
- (UIView *)titleView{
    if (!_titleView) {
        _titleView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - 30, 44)];
        _titleView.backgroundColor = [UIColor clearColor];
        UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake( 0, 5, SCREEN_WIDTH - 30, 34)];
        bgView.layer.masksToBounds = YES;
        bgView.layer.cornerRadius = 17;
        bgView.backgroundColor = [UIColor colorWithWhite:1 alpha:0.3];
        [_titleView addSubview:bgView];
        UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(15, 10, 15, 15)];
        image.image = [UIImage imageNamed:@"白色搜索"];
        [bgView addSubview:image];
        UIView *line = [[UIView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH - 30 - 48, 10, 1, 14)];
        line.backgroundColor = [UIColor whiteColor];
        [bgView addSubview:line];
        UIButton *cancle = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH - 30 - 50, 0, 49, 34)];
        [cancle addTarget:self action:@selector(CancelClik) forControlEvents:UIControlEventTouchUpInside];
        cancle.titleLabel.font = [UIFont fontWithName:@"PingFang-SC-Medium" size:13];
        [cancle setTitle:@"取消" forState:UIControlStateNormal];
        [cancle setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [bgView addSubview:cancle];
        self.Search = [[UITextField alloc]initWithFrame:CGRectMake(37, 0, SCREEN_WIDTH - 30 - 87, 34)];
        self.Search.clearButtonMode = UITextFieldViewModeWhileEditing;
        [self.Search addTarget:self action:@selector(SearchClik:) forControlEvents:UIControlEventEditingChanged];
        self.Search.placeholder = @"畅享合伙人";
        [self.Search setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
        self.Search.font = [UIFont fontWithName:@"PingFang-SC-Medium" size:13];
        self.Search.textColor = [UIColor whiteColor];
        [bgView addSubview:self.Search];
    }
    return _titleView;
}
- (void)CancelClik{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)SearchClik:(UITextField *)textfield{
    if (textfield.text.length > 0) {
        self.results = [[NSMutableArray alloc]init];
        for (NSString *subb in self.dataArr) {
            if ([subb containsString:textfield.text]) {
                [self.results addObject:subb];
            }
        }
        [self.tableView reloadData];
    }else{
        self.results = [[NSMutableArray alloc]initWithArray:self.dataArr copyItems:YES];
        [self.tableView reloadData];
    }
}
- (NSMutableArray *)results{
    if (!_results) {
        _results = [[NSMutableArray alloc]initWithArray:self.dataArr copyItems:YES];
    }
    return _results;
}
- (NSDictionary *)datadic{
    if (!_datadic) {
        _datadic = @{@"划拨":@"MyPOSlistVC",
                     @"回拨":@"POSCanTransferVC",
                     @"POS管理":@"TraditionPOSListVC",
                     @"钱包管理":@"NewWalletViewController",
                     @"银行卡管理":@"BandViewController",
                     @"提现":@"NewWalletViewController",
                     @"我的收益":@"MyProfitViewController",
                     @"提现记录":@"PutForwardRecordViewController"
                     };
    }
    return _datadic;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initCSS];
}
- (void)initCSS{
    [self SetNavBarSearch];
    self.dataArr = [[NSMutableArray alloc]initWithObjects:@"划拨",@"回拨",@"POS管理",@"钱包管理",@"银行卡管理",@"提现",@"我的收益",@"提现记录", nil];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.tableView registerClass:[HomeSearchCell class] forCellReuseIdentifier:@"HomeSearchCellID"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.sectionFooterHeight = 0.01f;
    self.tableView.sectionHeaderHeight = 0.01f;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
}
- (void)SetNavBarSearch{
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.hidesBackButton = YES;
    self.navigationItem.titleView = self.titleView;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.results.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 34;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HomeSearchCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HomeSearchCellID"];
    if (self.results.count > 0) {
        cell.Subtitle = self.results[indexPath.section];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    Class VCClass = NSClassFromString(self.datadic[self.results[indexPath.section]]);
    UIViewController *VC = [[VCClass alloc]init];
    [self.navigationController pushViewController:VC animated:YES];
    
}
@end
